add a u15 type that is 16bit only using 15bits
authorØyvind Kolås <pippin@gimp.org>
Wed, 2 May 2012 23:20:29 +0000 (01:20 +0200)
committerJon Nordby <jononor@gmail.com>
Sun, 13 May 2012 02:09:33 +0000 (19:09 -0700)
babl/base/Makefile.am
babl/base/babl-base.c
babl/base/babl-base.h
babl/base/model-gray.c
babl/base/model-rgb.c
babl/base/type-u15.c [new file with mode: 0644]

index 74052c2d5e006057e99784dc3638d7ccc2260504..55cd1df98d90610d7a4c0c7825015424ec76313a 100644 (file)
@@ -8,6 +8,7 @@ c_sources =                     \
        type-float.c            \
        type-half.c             \
        type-u8.c               \
+       type-u15.c              \
        type-u16.c              \
        type-u32.c              \
        model-rgb.c             \
index 0831eb144cd96d86564009fe4a3bfdc7a3cb6f44..3fe3cfc2288df06be6967c0e16e3cf5a9cce1b8f 100644 (file)
@@ -50,6 +50,7 @@ babl_base_destroy (void)
 static void
 types (void)
 {
+  babl_base_type_u15 ();
   babl_base_type_half ();
   babl_base_type_float ();
   babl_base_type_u8 ();
@@ -65,7 +66,9 @@ types (void)
 static void
 models (void)
 {
+  babl_hmpf_on_name_lookups--;
   babl_base_model_rgb ();
   babl_base_model_gray ();
+  babl_hmpf_on_name_lookups++;
   babl_base_model_ycbcr ();
 }
index f3bf6cd4205f27cf34f58a9c5ec085cb7f758c4d..588e2f13c2f6e9635760c017ccbfa43a2b738a59 100644 (file)
@@ -27,6 +27,7 @@ void babl_base_type_half   (void);
 void babl_base_type_float  (void);
 void babl_base_type_u8     (void);
 void babl_base_type_u16    (void);
+void babl_base_type_u15    (void);
 void babl_base_type_u32    (void);
 
 void babl_base_model_pal   (void);
index 057c831e093386f5fb485d7b43d2b4679c7ab87c..239333e1fc23826182428d8239e4c16c189fba23 100644 (file)
@@ -625,6 +625,43 @@ formats (void)
     babl_type_from_id (BABL_HALF),
     babl_component_from_id (BABL_LUMA),
     NULL);
+  /***********/
+
+  babl_format_new (
+    babl_model_from_id (BABL_GRAY_ALPHA),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_LUMINANCE),
+    babl_component_from_id (BABL_ALPHA),
+    NULL);
+  babl_format_new (
+    babl_model_from_id (BABL_GRAY_ALPHA_PREMULTIPLIED),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_LUMINANCE_MUL_ALPHA),
+    babl_component_from_id (BABL_ALPHA),
+    NULL);
+  babl_format_new (
+    babl_model_from_id (BABL_GRAY),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_LUMINANCE),
+    NULL);
+  babl_format_new (
+    babl_model_from_id (BABL_GRAY_GAMMA_2_2_ALPHA),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_LUMA),
+    babl_component_from_id (BABL_ALPHA),
+    NULL);
+  babl_format_new (
+    babl_model_from_id (BABL_GRAY_GAMMA_2_2_ALPHA_PREMULTIPLIED),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_LUMA_MUL_ALPHA),
+    babl_component_from_id (BABL_ALPHA),
+    NULL);
+  babl_format_new (
+    babl_model_from_id (BABL_GRAY_GAMMA_2_2),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_LUMA),
+    NULL);
+
   babl_format_new (
     babl_model_from_id (BABL_GRAY_ALPHA),
     babl_type_from_id (BABL_U32),
index 2d723432c250fbc30bb60cf38978fb3f578ca0c6..4ba6f7d3f224f6f6be2f04836e8e6865adfb1d80 100644 (file)
@@ -570,6 +570,59 @@ formats (void)
     babl_component_from_id (BABL_ALPHA),
     NULL);
 
+  /******/
+  babl_format_new (
+    babl_model_from_id (BABL_RGB),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_RED),
+    babl_component_from_id (BABL_GREEN),
+    babl_component_from_id (BABL_BLUE),
+    NULL);
+
+  babl_format_new (
+    babl_model_from_id (BABL_RGBA),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_RED),
+    babl_component_from_id (BABL_GREEN),
+    babl_component_from_id (BABL_BLUE),
+    babl_component_from_id (BABL_ALPHA),
+    NULL);
+
+  babl_format_new (
+    babl_model_from_id (BABL_RGBA_PREMULTIPLIED),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_RED_MUL_ALPHA),
+    babl_component_from_id (BABL_GREEN_MUL_ALPHA),
+    babl_component_from_id (BABL_BLUE_MUL_ALPHA),
+    babl_component_from_id (BABL_ALPHA),
+    NULL);
+
+  babl_format_new (
+    babl_model_from_id (BABL_RGB_GAMMA_2_2),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_RED_GAMMA_2_2),
+    babl_component_from_id (BABL_GREEN_GAMMA_2_2),
+    babl_component_from_id (BABL_BLUE_GAMMA_2_2),
+    NULL);
+
+  babl_format_new (
+    babl_model_from_id (BABL_RGBA_GAMMA_2_2),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_RED_GAMMA_2_2),
+    babl_component_from_id (BABL_GREEN_GAMMA_2_2),
+    babl_component_from_id (BABL_BLUE_GAMMA_2_2),
+    babl_component_from_id (BABL_ALPHA),
+    NULL);
+
+  babl_format_new (
+    babl_model_from_id (BABL_RGBA_GAMMA_2_2_PREMULTIPLIED),
+    babl_type ("u15"),
+    babl_component_from_id (BABL_RED_GAMMA_2_2_MUL_ALPHA),
+    babl_component_from_id (BABL_GREEN_GAMMA_2_2_MUL_ALPHA),
+    babl_component_from_id (BABL_BLUE_GAMMA_2_2_MUL_ALPHA),
+    babl_component_from_id (BABL_ALPHA),
+    NULL);
+
 
   babl_format_new (
     babl_model_from_id (BABL_RGB),
diff --git a/babl/base/type-u15.c b/babl/base/type-u15.c
new file mode 100644 (file)
index 0000000..47e3878
--- /dev/null
@@ -0,0 +1,136 @@
+/* babl - dynamically extendable universal pixel conversion library.
+ * Copyright (C) 2005, Øyvind Kolås.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include <string.h>
+#include <stdint.h>
+#include <assert.h>
+#include <math.h>
+
+#include "babl-internal.h"
+#include "babl-base.h"
+
+
+static inline long
+convert_double_u15_scaled (double   min_val,
+                           double   max_val,
+                           uint16_t min,
+                           uint16_t max,
+                           char    *src,
+                           char    *dst,
+                           int      src_pitch,
+                           int      dst_pitch,
+                           long     n)
+{
+  while (n--)
+    {
+      double   dval = *(double *) src;
+      uint16_t u15val;
+
+      if (dval < min_val)
+        u15val = min;
+      else if (dval > max_val)
+        u15val = max;
+      else
+        u15val = rint ((dval - min_val) / (max_val - min_val) * (max - min) + min);
+
+      *(uint16_t *) dst = u15val;
+      dst              += dst_pitch;
+      src              += src_pitch;
+    }
+  return n;
+}
+
+static inline long
+convert_u15_double_scaled (double   min_val,
+                           double   max_val,
+                           uint16_t min,
+                           uint16_t max,
+                           char    *src,
+                           char    *dst,
+                           int      src_pitch,
+                           int      dst_pitch,
+                           long     n)
+{
+  while (n--)
+    {
+      int    u15val = *(uint16_t *) src;
+      double dval;
+
+      if (u15val < min)
+        dval = min_val;
+      else if (u15val > max)
+        dval = max_val;
+      else
+        dval = (u15val - min) / (double) (max - min) * (max_val - min_val) + min_val;
+
+      (*(double *) dst) = dval;
+      dst              += dst_pitch;
+      src              += src_pitch;
+    }
+  return n;
+}
+
+#define MAKE_CONVERSIONS(name, min_val, max_val, min, max)      \
+  static long \
+  convert_ ## name ## _double (void *src, \
+                               void *dst, \
+                               int src_pitch, \
+                               int dst_pitch, \
+                               long n)                               \
+  { \
+    return convert_u15_double_scaled (min_val, max_val, min, max, \
+                                      src, dst, src_pitch, dst_pitch, n); \
+  }                                                               \
+  static long \
+  convert_double_ ## name (void *src, \
+                           void *dst, \
+                           int src_pitch, \
+                           int dst_pitch, \
+                           long n)                                 \
+  { \
+    return convert_double_u15_scaled (min_val, max_val, min, max, \
+                                      src, dst, src_pitch, dst_pitch, n); \
+  }
+
+MAKE_CONVERSIONS (u15, 0.0, 1.0, 0, (1<<15))
+
+void
+babl_base_type_u15 (void)
+{
+  babl_hmpf_on_name_lookups--;
+  babl_type_new (
+    "u15",
+    "bits", 16,
+    NULL);
+
+  babl_conversion_new (
+    babl_type ("u15"),
+    babl_type_from_id (BABL_DOUBLE),
+    "plane", convert_u15_double,
+    NULL
+  );
+
+  babl_conversion_new (
+    babl_type_from_id (BABL_DOUBLE),
+    babl_type ("u15"),
+    "plane", convert_double_u15,
+    NULL
+  );
+  babl_hmpf_on_name_lookups++;
+}